rm(list=ls()) 


library(foreign)
library(mediation)

data<-read.dta("prov_mediate_12.dta")
attach(data)


region=cbind(Iregion_2,Iregion_3,Iregion_4,Iregion_5,Iregion_6,Iregion_7,Iregion_8,Iregion_9,Iregion_10,Iregion_11,Iregion_12,Iregion_13,Iregion_14,Iregion_15,Iregion_16,Iregion_17,Iregion_18,Iregion_19,Iregion_20,Iregion_21,Iregion_22,Iregion_23,Iregion_24,Iregion_25)

country<-cbind(Igmi_code_2,Igmi_code_3,Igmi_code_4,Igmi_code_5,Igmi_code_6,Igmi_code_7,Igmi_code_8,Igmi_code_9,Igmi_code_10,Igmi_code_11,Igmi_code_12,Igmi_code_13,Igmi_code_14,Igmi_code_15,Igmi_code_16,Igmi_code_17,Igmi_code_18,Igmi_code_19,Igmi_code_20,Igmi_code_21,Igmi_code_22,Igmi_code_23,Igmi_code_24,Igmi_code_25,Igmi_code_26,Igmi_code_27,Igmi_code_28,Igmi_code_29,Igmi_code_30,Igmi_code_31,Igmi_code_32,Igmi_code_33,Igmi_code_34,Igmi_code_35,Igmi_code_36,Igmi_code_37,Igmi_code_38,Igmi_code_39,Igmi_code_40,Igmi_code_41,Igmi_code_42,Igmi_code_43,Igmi_code_44,Igmi_code_45,Igmi_code_46,Igmi_code_47,Igmi_code_48,Igmi_code_49,Igmi_code_50,Igmi_code_51,Igmi_code_52,Igmi_code_53,Igmi_code_54,Igmi_code_55,Igmi_code_56,Igmi_code_57,Igmi_code_58,Igmi_code_59,Igmi_code_60,Igmi_code_61,Igmi_code_62,Igmi_code_63,Igmi_code_64,Igmi_code_65,Igmi_code_66,Igmi_code_67,Igmi_code_68,Igmi_code_69,Igmi_code_70,Igmi_code_71,Igmi_code_72,Igmi_code_73,Igmi_code_74,Igmi_code_75,Igmi_code_76,Igmi_code_77,Igmi_code_78,Igmi_code_79,Igmi_code_80,Igmi_code_81,Igmi_code_82,Igmi_code_83,Igmi_code_84,Igmi_code_85,Igmi_code_86,Igmi_code_87,Igmi_code_88,Igmi_code_89,Igmi_code_90,Igmi_code_91,Igmi_code_92,Igmi_code_93,Igmi_code_94,Igmi_code_95,Igmi_code_96,Igmi_code_97,Igmi_code_98,Igmi_code_99,Igmi_code_100,Igmi_code_101,Igmi_code_102,Igmi_code_103,Igmi_code_104,Igmi_code_105,Igmi_code_106,Igmi_code_107,Igmi_code_108,Igmi_code_109,Igmi_code_110,Igmi_code_111,Igmi_code_112,Igmi_code_113,Igmi_code_114,Igmi_code_115,Igmi_code_116,Igmi_code_117,Igmi_code_118,Igmi_code_119,Igmi_code_120,Igmi_code_121,Igmi_code_122,Igmi_code_123,Igmi_code_124,Igmi_code_125,Igmi_code_126,Igmi_code_127,Igmi_code_128,Igmi_code_129,Igmi_code_130,Igmi_code_131,Igmi_code_132,Igmi_code_133,Igmi_code_134,Igmi_code_135,Igmi_code_136,Igmi_code_137,Igmi_code_138,Igmi_code_139,Igmi_code_140,Igmi_code_141,Igmi_code_142,Igmi_code_143,Igmi_code_144,Igmi_code_145,Igmi_code_146,Igmi_code_147 ,Igmi_code_148,Igmi_code_149,Igmi_code_150,Igmi_code_151,Igmi_code_152,Igmi_code_153,Igmi_code_154,Igmi_code_155,Igmi_code_156,Igmi_code_157,Igmi_code_158,Igmi_code_159,Igmi_code_160,Igmi_code_161,Igmi_code_162,Igmi_code_163,Igmi_code_164,Igmi_code_165,Igmi_code_166,Igmi_code_167,Igmi_code_168,Igmi_code_169,Igmi_code_170,Igmi_code_171,Igmi_code_172,Igmi_code_173,Igmi_code_174,Igmi_code_175,Igmi_code_176,Igmi_code_177,Igmi_code_178,Igmi_code_179,Igmi_code_180,Igmi_code_181,Igmi_code_182,Igmi_code_183,Igmi_code_184,Igmi_code_185,Igmi_code_186,Igmi_code_187,Igmi_code_188,Igmi_code_189,Igmi_code_190,Igmi_code_191 ,Igmi_code_192,Igmi_code_193,Igmi_code_194,Igmi_code_195, Igmi_code_196,Igmi_code_197,Igmi_code_198,Igmi_code_199,Igmi_code_200,Igmi_code_201,Igmi_code_202,Igmi_code_203,Igmi_code_204,Igmi_code_205,Igmi_code_206,Igmi_code_207,Igmi_code_208,Igmi_code_209,Igmi_code_210,Igmi_code_211,Igmi_code_212,Igmi_code_213,Igmi_code_214,Igmi_code_215,Igmi_code_216,Igmi_code_217,Igmi_code_218,Igmi_code_219,Igmi_code_220,Igmi_code_221,Igmi_code_222,Igmi_code_223,Igmi_code_224,Igmi_code_225,Igmi_code_226,Igmi_code_227,Igmi_code_228,Igmi_code_229,Igmi_code_230,Igmi_code_231,Igmi_code_232,Igmi_code_233,Igmi_code_234,Igmi_code_235,Igmi_code_236,Igmi_code_237,Igmi_code_238,Igmi_code_239,Igmi_code_240)


####
####  NOTE: THE ESTIMATES FROM THE MEDIATION MODELS WILL DIFFER VERY SLIGHTLY EACH TIME THE MEDIATION MODELS ARE ESTIMATED AS THE ESTIMATES ARE BASED ON BOOTSTRAP DRAWS
####
    

excl_eq_r<- lm(excluded_sum~ruggbin_90+region, data=data)
summary(excl_eq_r)

civwar_eq_r <- lm(civwar_sum~excluded_sum+ruggbin_90+ region, data= data)
summary(civwar_eq_r)

mediation_r <- mediate(excl_eq_r, civwar_eq_r, sims=500, 
                    treat="ruggbin_90", mediator="excluded_sum")
summary(mediation_r)


excl_eq_c<- lm(excluded_sum~ruggbin_90+country, data=data)
summary(excl_eq_c)

civwar_eq_c <- lm(civwar_sum~excluded_sum+ruggbin_90+ country, data= data)
summary(civwar_eq_c)

mediation_c <- mediate(excl_eq_c, civwar_eq_c, sims=500, 
                    treat="ruggbin_90", mediator="excluded_sum")
summary(mediation_c)

